clear
* Change directory to where the folder with the data is
cd "Data Archive TWT Predictions\KTHDP"

use DatasetKTHDP.dta,clear

*Make sure the dataset is sorted in the same way as the datasets containing the CDFs estimations.
sort rt, stable
*Merge estimated CDFs.
merge 1:1 subj trial using LeftChoice, nogenerate
merge 1:1 subj trial using RightChoice, nogenerate
sort rt, stable

*Generate choice proportions
gen ratioA = sumleft/repetition
gen ratioB = sumright/repetition
sum sumleft sumright ratioA ratioB
gen sumA = sumleft
gen sumB = sumright
gen pchosen = ratioB / ratioA if ratioA>= ratioB
replace pchosen = ratioA / ratioB if ratioA<ratioB
tab pchosen

gen RTordered=rt
* Loop over subjects and pairs to calculate the RTs threshold given the CDF and choice proportion.
 local j = 1
          while `j' < 31{
 local i = 1
          while `i' < 46{
        capture gen RtpredA`i'  = .
        capture confirm variable cdfAsubj`j'id`i'
        if !_rc {
            sort cdfAsubj`j'id`i'
			sum pchosen if subj == `j' & idpair == `i'
            gen LowerBoundsubj`j'id`i'=(cdfAsubj`j'id`i'<=r(mean))
			sum cdfAsubj`j'id`i', detail
			replace LowerBoundsubj`j'id`i'=1 if cdfAsubj`j'id`i'== r(min)
            gen UpperBoundsubj`j'id`i'=(cdfAsubj`j'id`i'>r(mean)) 
			sum cdfAsubj`j'id`i', detail
			replace UpperBoundsubj`j'id`i'=1 if cdfAsubj`j'id`i'==r(max)
            gen TresholdsubLowerj`j'id`i'=(LowerBoundsubj`j'id`i'+LowerBoundsubj`j'id`i'[_n-1])
            gen TresholdsubUpperj`j'id`i'=(UpperBoundsubj`j'id`i'+UpperBoundsubj`j'id`i'[_n+1])
            sum RTordered if TresholdsubLowerj`j'id`i'==1 | TresholdsubUpperj`j'id`i'==1, detail
            replace RtpredA`i'  = r(mean) if subj==`j'
           drop LowerBoundsubj`j'id`i' UpperBoundsubj`j'id`i' TresholdsubLowerj`j'id`i' TresholdsubUpperj`j'id`i'
		   }
        capture gen RtpredB`i'  = .
        capture confirm variable cdfBsubj`j'id`i'
        if !_rc {
            sort cdfBsubj`j'id`i'
			sum pchosen if subj == `j' & idpair == `i'
            gen LowerBoundsubj`j'id`i'=(cdfBsubj`j'id`i'<=r(mean))
			sum cdfBsubj`j'id`i', detail
			replace LowerBoundsubj`j'id`i'=1 if cdfBsubj`j'id`i'== r(min)
            gen UpperBoundsubj`j'id`i'=(cdfBsubj`j'id`i'>r(mean)) 
			sum cdfBsubj`j'id`i', detail
			replace UpperBoundsubj`j'id`i'=1 if cdfBsubj`j'id`i'==r(max)
            gen TresholdsubLowerj`j'id`i'=(LowerBoundsubj`j'id`i'+LowerBoundsubj`j'id`i'[_n-1])
            gen TresholdsubUpperj`j'id`i'=(UpperBoundsubj`j'id`i'+UpperBoundsubj`j'id`i'[_n+1])
            sum RTordered if TresholdsubLowerj`j'id`i'==1 | TresholdsubUpperj`j'id`i'==1, detail
            replace RtpredB`i'  = r(mean) if subj==`j'  & idpair==`i'
           drop LowerBoundsubj`j'id`i' UpperBoundsubj`j'id`i' TresholdsubLowerj`j'id`i' TresholdsubUpperj`j'id`i'
        }		
local i = `i' + 1
            }			
local j = `j' + 1
            }

*Calculate predicted RTs from TWT				
*This is an out-of-sample estimation, exclude pairs used for the estimation from the comparisons
local i = 1
          while `i' < 6{
local j = 1
          while `j' < 6{		  
capture: gen RTpredu`i't`j'=   RtpredA`i' if idright==`j' & idleft==`i'				  
capture: replace RTpredu`j't`i'= RtpredA`i' if idleft==`j' & idright==`i'		
local j = `j' + 1
            }
local i = `i' + 1
            }
						
sort subj idpair idleft	
*Collapse dataset at the subject and pair level
collapse idleft idright RTpredu* ratiou* choiceleft, by(subj idpair)

*Create binary choices out of choice proportions
gen Choiceleft =0 if choiceleft<0.5
replace Choiceleft=1 if choiceleft>0.5	

*Create pair and subject specific variables for the predicted RTs	
local i = 1
          while `i' < 6{
local j = 1
          while `j' < 6{		  
egen Ratiou`i't`j'= mean(ratiou`i't`j'), by(subj)
egen RTPredu`i't`j'= mean(RTpredu`i't`j'), by(subj)
replace ratiou`i't`j'= Ratiou`i't`j'
replace RTpredu`i't`j'= RTPredu`i't`j'
local j = `j' + 1
            }		
local i = `i' + 1
            }	
*Drop variables used just for the loop			
drop 	Ratiou* RTPredu*	
*Save base prediction dataset			
save b, replace

*In KTHDP each lottery is compared to all others. To make an out-of-sample estimation one needs to repeat the predictions excluding pairs and then average the predictive performance across estimations.
*Predictions in TWT are based on the comparisons of the predicted RTs.
	preserve	
local x = 1
          while `x' < 6{	
use b, clear	
gen left=.
local i = 1
          while `i' < 6{	
local j = 1
          while `j' < 6{
local k = 1
          while `k' < 6{		  
	capture{		
	    *Predictions using Response times (cases 3 and 4 of Theorem 1 in the paper)
replace left = 0 if  RTpredu`i't`j'< RTpredu`i't`k' & idleft!=`x' & idright!=`x' & idleft==`j' & idright==`k' & idleft!=idright
replace left = 1 if  RTpredu`i't`j'> RTpredu`i't`k' & idleft!=`x' & idright!=`x' & idleft==`j' & idright==`k' & idleft!=idright
        *Predictions using transitivity (cases 1 and 2 of Theorem 1 in the paper)
replace left = 0 if  ratiou`i't`j'<0.5 &  ratiou`i't`k'>0.5 & idleft!=`x' & idright!=`x' & idleft==`j' & idright==`k' & idleft!=idright
replace left = 1 if  ratiou`i't`j'>0.5 &  ratiou`i't`k'<0.5 & idleft!=`x' & idright!=`x' & idleft==`j' & idright==`k' & idleft!=idright
	}
local k = `k' + 1
            } 	
local j = `j' + 1
            }			
local i = `i' + 1
            }

*Create the correctly-predicted variable
gen corrpredTWT`x' = 0     if  left==1 & Choiceleft==0   & left!=. & choiceleft!=.
replace corrpredTWT`x' = 0 if  left==0 & Choiceleft==1   & left!=. & choiceleft!=.
replace corrpredTWT`x' =1  if  left==1 & Choiceleft==1	 & left!=. & choiceleft!=.		
replace corrpredTWT`x' =1  if  left==0 & Choiceleft==0	 & left!=. & choiceleft!=.		
collapse corrpredTWT`x', by(subj)
sum corrpredTWT`x'
save propcorrpred`x'TWTTheorem2, replace
local x = `x' + 1
            }
restore

*Merge all datasets
use  propcorrpred1TWTTh2
capture{
merge 1:1 subj using  propcorrpred2TWTTh2, nogen
merge 1:1 subj using  propcorrpred3TWTTh2, nogen
merge 1:1 subj using  propcorrpred4TWTTh2, nogen
merge 1:1 subj using  propcorrpred5TWTTh2, nogen
}
sum corrpredTWT*
save propcorrpredTWTTh2, replace